﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace FacialClub.Helpers
{
    /// <summary>
    /// CustomValidationExtensions
    /// </summary>
    public static class CustomValidationExtensions
    {
        /// <summary>
        /// ValidationSummaryJQuery
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="message"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ValidationSummaryJQuery(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes)
        {
            if (htmlHelper != null)
                if (!htmlHelper.ViewData.ModelState.IsValid)
                    return htmlHelper.ValidationSummary(message, htmlAttributes); // use default Validation Summary rendering if ModelState is invalid

            var sb = new StringBuilder(Environment.NewLine);

            var divBuilder = new TagBuilder("div") {InnerHtml = sb.ToString()};
            divBuilder.MergeAttributes<string, object>(htmlAttributes);
            divBuilder.AddCssClass(HtmlHelper.ValidationSummaryValidCssClassName); // intentionally add VALID css class

            if (!string.IsNullOrEmpty(message))
            {
                var spanBuilder = new TagBuilder("span");
                spanBuilder.MergeAttribute("style", "display:none");
                spanBuilder.SetInnerText(message);
                sb.Append(spanBuilder.ToString(TagRenderMode.Normal)).Append(Environment.NewLine);
            }
            return MvcHtmlString.Create(divBuilder.ToString(TagRenderMode.Normal));
        }
    }
}